package com.icas.service;

import com.icas.entity.databaseentity.AccountEntity;
import com.icas.util.Constant;
import com.icas.util.md5.PasswordRules;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.util.List;

import static com.icas.util.Constant.USER_TABLE_NAME;

/**
 * @Author: tocchen
 * @Date: 2022/4/18 18:47
 * @Description: 账户处理的具体实现——登录和修改密码
 **/
@Component
public class AccountService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public static AccountService db;

    public static final AccountService defaultUserServer = new AccountService();

    @PostConstruct
    public void init() {
        db = this;
        db.mongoTemplate = this.mongoTemplate;
    }

    /**
     * 登录操作
     * @param account 账号
     * @param password 密码
     * @return 返回登录的情况
     */
    public AccountEntity login(String account,String password){

        if ("".equals(account)||"".equals(password)|| ObjectUtils.isEmpty(account)||ObjectUtils.isEmpty(password)){
            return null;
        }

        AccountEntity info = byAccountGetInfo(account);
        if (ObjectUtils.isEmpty(info)){
            return null;
        }

        Md5Hash md5Hash = new Md5Hash(password, info.getSalt(), Constant.HASH_NUM);
        if (!info.getPassword().equals(md5Hash.toHex())){
            return null;
        }

        return info;
    }

    /**
     * 更改密码
     * @param id id值
     * @param password 新的密码值
     * @return 返回修改后的结果
     */
    public Boolean updatePwd(String id,String password){
        Query query = new Query(Criteria.where("_id").is(new ObjectId(id)));
        Update update = new Update();
        List passwordList = PasswordRules.genPassword(password);
        if (passwordList.size() != Constant.PASSWORD_RULE_LEN || ObjectUtils.isEmpty(passwordList)){
            return Boolean.FALSE;
        }
        update.set("salt",passwordList.get(0));
        update.set("passcode",passwordList.get(1));
        try{
            db.mongoTemplate.updateFirst(query,update,USER_TABLE_NAME);
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     * 通过账号查询用户实例
     * @param account 账号
     * @return 所带账号的实体类
     */
    public AccountEntity byAccountGetInfo(String account){
        Query query = new Query();
        query.addCriteria(Criteria.where("account").is(account));
        AccountEntity users = db.mongoTemplate.findOne(query, AccountEntity.class, USER_TABLE_NAME);
        return ObjectUtils.isEmpty(users)?null:users;
    }

    /**
     * 通过id值查询用户实例
     * @param id id值
     * @return {@link AccountEntity} 实例
     */
    public AccountEntity byIdGetInfo(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(id)));
        AccountEntity users = db.mongoTemplate.findOne(query, AccountEntity.class, USER_TABLE_NAME);
        return ObjectUtils.isEmpty(users)?null:users;
    }



}
